﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseTank : MonoBehaviour
{
    //坦克模型
    private GameObject skin;
    
    //转向速度
    public float steer = 20f;
    //移动速度
    public float speed = 10f;
    
    public float turretSpeed = 30f;
    
    public Transform turret;
    public Transform gun;
    public Transform firePoint;

    public float fireCD = 0.5f;
    public float lastFireTime = 0;

    protected Rigidbody rigidbody;

    public float hp = 100;

    public string id = "";
    public int camp = 0;
    public bool IsDie()
    {
        return hp<=0;
    }

    // Start is called before the first frame update
    public void Start()
    {
        
    }

    // Update is called once per frame
    public void Update()
    {
        
    }

    public virtual void Init(string skinPath)
    {
        GameObject skinRes = ResManager.LoadPrefab(skinPath);
        skin = Instantiate(skinRes);
        skin.transform.parent = this.transform;
        this.transform.position = new Vector3(0, 10, 0);
        skin.transform.localPosition=new Vector3(0,0,0);
        skin.transform.localEulerAngles=Vector3.zero;

        rigidbody = gameObject.AddComponent<Rigidbody>();
        BoxCollider boxCollider = gameObject.AddComponent<BoxCollider>();
        boxCollider.center = new Vector3(0, 2.5f, 1.47f);
        boxCollider.size = new Vector3(7, 5, 12);
        
        turret = skin.transform.Find("Turret");
        gun = turret.transform.Find("Gun");
        firePoint = gun.transform.Find("FirePoint");
    }

    public Bullet Fire()
    {
        if (IsDie())
        {
            return null;
        }

        GameObject bulletObj = new GameObject("bullet");
        Bullet bullet = bulletObj.AddComponent<Bullet>();
        bullet.Init();
        bullet.tank = this; 

        bullet.transform.position = firePoint.position;
        bullet.transform.rotation = firePoint.rotation;
        
        GameObject fire = ResManager.LoadPrefab("fire");
        Instantiate(fire, firePoint.position, firePoint.rotation);

        lastFireTime = Time.time;
        return bullet;
    }
    
    public void Attacked(float att){
        //已经死亡
        if(IsDie()){
            return;
        }
        //扣血
        hp -= att;
        //死亡
        if(IsDie()){
            //显示焚烧效果
            GameObject obj = ResManager.LoadPrefab("brokenfire");
            GameObject explosion= Instantiate(obj, transform.position, transform.rotation);
            explosion.transform.SetParent(transform);
        }
    }
}
